import {  createSlice, Draft, PayloadAction } from "@reduxjs/toolkit";
import { persistReducer } from "redux-persist";
import storage from "redux-persist/lib/storage";

// Partial<T> 是一个内置的工具类型，它的作用是将类型 T 里的所有属性转换为可选属性
export interface UserSlice {
  user: Partial<User>;
}

const initialState:UserSlice=  {
    user: {},
  }
const { reducer: userReducer, actions } = createSlice({
  name: "user",
  initialState:initialState,
  reducers: {
    saveUser(state:Draft<UserSlice>, action: PayloadAction<Partial<User>>) {
      state.user = action.payload;
    },
  },
});

// 持久化
const persistUserReducer = persistReducer(
  { key: "userReducer", storage },
  userReducer
);
export const { saveUser } = actions;
export default persistUserReducer;
